/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdp.cdf.component.manager.serializer.CompOperationSerializer;
import com.inspur.edp.cdp.cdf.component.manager.serializer.ComponentSerializer;
import com.inspur.edp.component.schema.AbstractComponent;
import com.inspur.edp.component.schema.IGspCompResource;
import com.inspur.edp.webservicecomponent.metadata.WebServiceComponent;
import com.inspur.edp.webservicecomponent.metadata.sourceInfo.DeprecatedSourceInfoDeserializer;

public class WebServiceCmpSerializer extends ComponentSerializer {

    @Override
    protected void readExtendInfo(AbstractComponent comp, JsonNode jsonNode) {
        //旧数据结构存在此字段，需要单独反序列化赋值(新版元数据已直接使用构件基类相应字段)
        if (JsonUtil.isExistsProp(jsonNode, "sourceInfo")) {
            JsonNode sourceInfoNode = JsonUtil.getPropValue(jsonNode, "sourceInfo", JsonNode.class);
            IGspCompResource sourceInfo = DeprecatedSourceInfoDeserializer.deserialize(sourceInfoNode);
            comp.setResource(sourceInfo);
        }
    }

    @Override
    protected void writeExtendInfo(AbstractComponent comp, JsonNode jsonNode) {
        if (comp.getResource() == null) {
            return;
        }
        //当前设计器页面未改动，需保留原有sourceinfo节点
        JsonNode sourceInfoNode = DeprecatedSourceInfoDeserializer.serialize(comp.getResource());
        JsonUtil.setPropValue(jsonNode, "sourceInfo", sourceInfoNode);
    }

    @Override
    protected CompOperationSerializer getOperationSerializer() {
        return new WebServiceOperationSerializer();
    }

    @Override
    protected AbstractComponent createComponent() {
        return new WebServiceComponent();
    }
}
